woyouhuo = {
	baseUrl: "",
	currencySign: "￥",
	currencyUnit: "元",
	priceScale: "2",
	priceRoundType: "roundHalfUp"
};
var httpImageUrl;
if(window.location.host == 'm.sfddj.com' || window.location.host == '120.76.47.184' || window.location.host == '120.76.207.85' || window.location.host == '120.76.44.28') {
	httpImageUrl = "https://img.sfddj.com/";
}
else if (window.location.host == 'hdm.sfddj.com') {
	httpImageUrl = "https://img.sfddj.com/";
}
else if (window.location.host == 'itsm.sfddj.com') {
	httpImageUrl = "https://img.fx-sf.com/";
}
else if (window.location.host == 'shop.fx-sf.com') {
	httpImageUrl = "https://img.fx-sf.com/";
}
else if(window.location.host == 'www.shhyxypsx.com') {
	httpImageUrl = "https://img.fx-sf.com/";
}
else if(window.location.hostname == 'localhost') {
	httpImageUrl = "http://img-dev.sfddj.com/";
}

//货币格式化 88.88
function priceFormat(price) {
	price = setScale(price, woyouhuo.priceScale, woyouhuo.priceRoundType);
	return price;
}

//浮点数加法运算
function floatAdd(arg1, arg2) {
	var r1, r2, m;
	try{
		r1 = arg1.toString().split(".")[1].length;
	} catch(e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch(e) {
		r2 = 0;
	}
	m = Math.pow(10, Math.max(r1, r2));
	return (arg1 * m + arg2 * m) / m;
}

// 浮点数减法运算
function floatSub(arg1, arg2) {
	var r1, r2, m, n;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch(e) {
		r1 = 0
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch(e) {
		r2 = 0
	}
	m = Math.pow(10, Math.max(r1, r2));
	n = (r1 >= r2) ? r1 : r2;
	return ((arg1 * m - arg2 * m) / m).toFixed(n);
}

// 浮点数乘法运算
function floatMul(arg1, arg2) {
	var m = 0, s1 = arg1.toString(), s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length;
	} catch(e) {}
	try {
		m += s2.split(".")[1].length;
	} catch(e) {}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}

// 浮点数除法运算
function floatDiv(arg1, arg2) {
	var t1 = 0, t2 = 0, r1, r2;    
	try {
		t1 = arg1.toString().split(".")[1].length;
	} catch(e) {}
	try {
		t2 = arg2.toString().split(".")[1].length;
	} catch(e) {}
	with(Math) {
		r1 = Number(arg1.toString().replace(".", ""));
		r2 = Number(arg2.toString().replace(".", ""));
		return (r1 / r2) * pow(10, t2 - t1);
	}
}

// 设置数值精度
function setScale(value, scale, roundingMode) {
	if (roundingMode.toLowerCase() == "roundhalfup") {
		return (Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	} else if (roundingMode.toLowerCase() == "roundup") {
		return (Math.ceil(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	} else {
		return (Math.floor(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	}
}

//设置cookie时间
function getsec(str) {
    var str1=str.substring(1,str.length)*1;
    var str2=str.substring(0,1);
    if (str2=="s") {
        return str1*1000;
    }
    else if (str2=="h") {
        return str1*60*60*1000;
    }
    else if (str2=="d") {
        return str1*24*60*60*1000;
    }
}

//s20是代表20秒 h是指小时，如12小时则是：h12 d是天数，30天则：d30
//写cookies setCookie("name","hayden",'s20');
function setCookie(name,value,time) {
    var strsec;
    if (time == undefined) {
        var Days = 30; //默认30天有效期
        strsec = Days*24*60*60*1000;
    }
    else {
        strsec = getsec(time)*1;
    }
    var exp = new Date();
    exp.setTime(exp.getTime() + strsec);
    document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString() + ";path=/";
}
//读取cookies getCookie("name")
function getCookie(name) {
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg)) {
        return unescape(arr[2]);
    }
    else{
        return null;
    }

}
//删除cookies delCookie('name')
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=getCookie(name);
    if(cval!=null)
        document.cookie = name + "="+cval+";expires="+exp.toGMTString() + ";path=/";
}

/**
 * 如果用户session过期 跳转到登录授权或登录页
 */
function goLoginAuth(){
    var locationHref=window.location.href;
    setCookie("historyLastUrl",locationHref,'d3');
    if(isWeiXin()){
        $.ajax({
            url: woyouhuo.baseUrl+'/common/getWxAuth',
            type: 'POST',
            dataType: "json",
            success: function(data) {
                var appid=data.appId;
                var redirect_uri=data.redirect_uri+'?redirect_url='+locationHref;
                window.location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid='+appid+'&redirect_uri='+encodeURI(redirect_uri)
                    +'&response_type=code&scope=snsapi_userinfo&state=sfddj#wechat_redirect';
            }
        });
    }else if(isAlipayClient()){
        $.ajax({
            url: woyouhuo.baseUrl+'/common/getAlipayAuth',
            type: 'POST',
            dataType: "json",
            success: function(data) {
                var appid=data.appId;
                var redirect_uri=data.redirect_uri+'?redirect_url='+locationHref;
                window.location.href='https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id='+appid+'&scope=auth_userinfo&redirect_uri='+encodeURI(redirect_uri);
            }
        });
	}else if(isAicityClient()){
        $.ajax({
            url: woyouhuo.baseUrl+'/common/getAicityAuth',
            type: 'POST',
            dataType: "json",
            success: function(data) {
                var appid=data.app_id;
                var redirect_uri=data.redirect_uri;
                var authorize_uri=data.authorize_uri;
                window.location.href=authorize_uri+'?app_id='+appid+'&scope=base_info&redirect_uri='+redirect_uri+'&state='+locationHref;
            }
        });
    }

	else{
        window.location.href=woyouhuo.baseUrl+"/h/login/";
    }
}


/**
 * 
 * @param url   请求地址
 * @param data   提交的数据
 * @param callBack success回调函数
 */
function G_JSON_POST(url,data,callBack,start,over) {
	var _Data = [];
	$.ajax({
		url: woyouhuo.baseUrl+url,
		data: data,
		type: 'POST',
		dataType: "json",
		beforeSend:function() {if(start)start(data);},
		success: function(data) {
			_Data = eval(data);
			callBack(data);
		},
		error:function(x){
			if(403==x.status){
                goLoginAuth();
			}
			if(429==x.status){
				window.location.href = httpImageUrl+ 'errorPage/429.html';
			}
			console.log(x.status);
		},
		complete:function (XHR,TS) {
			if(over)over(_Data);
			XHR = null;
			
		}
	});
}

function G_JSON_GET(url,data,callBack,start,over) {
	var _Data = [];
	$.ajax({
		url: woyouhuo.baseUrl+url,
		data: data,
		type: 'GET',
		dataType: "json",
		contentType: "application/json; charset=utf-8",
		beforeSend:function() {if(start)start(data);},
		success: function(data) {
			_Data = eval(data);
			callBack(data);
		},
		error:function(x){
			if(403==x.status){
                goLoginAuth();
			}

			if(429==x.status){
				window.location.href = httpImageUrl+ 'errorPage/429.html';
			}
			console.log(x.status);
		},
		complete:function (XHR,TS) {
			if(over)over(_Data);
			XHR = null; 
		}
	});
}





/**
 * 判断是否是微信浏览器
 * @returns {Boolean}
 */
function isWeiXin() {
	var ua = window.navigator.userAgent.toLowerCase();
	if (ua.match(/MicroMessenger/i) == 'micromessenger') {
		return true;
	} else {
		return false;
	}
}
/**
 * 判断是否是支付宝浏览器
 * @returns {boolean}
 */
function isAlipayClient() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.match(/AlipayClient/i) == 'alipayclient') {
        return true;
    } else {
        return false;
    }
}
/**
 * 判断是否是支付宝浏览器
 * @returns {boolean}
 */
function isAicityClient() {
    var ua = window.navigator.userAgent.toLowerCase();
    if (ua.indexOf("Inspur ICity")>-1) {
        return true;
    } else {
        return false;
    }
}




function tipDialog(msg){
	
	  var $wrap = $('<div class="tip-box">'
			  +'<div class="tip-con">'
			   +msg+'</div></div>');
	  $("body").append($wrap);

	  $wrap.fadeIn(1200);

	  $wrap.click(function(){
	    $wrap.fadeOut(1200).remove();
	  })

	  setTimeout(function(){
	    $wrap.fadeOut(1000).remove();
	    },1300);
}

function getGoodsLabels(goodslabel){
	if("NEW"==goodslabel){
		return "tip-new";
	}else if("XIANSHI"==goodslabel){
		return "tip-timeAct timeAct";
	}else if("ZSONG"==goodslabel){
		return "tip-give";
	}else if("HOT"==goodslabel){
		return "tip-hot";
	}else if("TEJIA"==goodslabel){
		return "tip-tj";
	}else{
	    return "";
	}
}


function checkMobilePhone(v){
    var reg = /^(1[1|2|3|4|5|6|7|8|9])[\d]{9}$/;   //验证手机号码
	if(!reg.test(v)){
		return false;
	}
	return true;
}



$().ready( function() {
	
//加入购物车
$.addToCart = function(id,quantity){
	if($.trim(id) == ""){
		alert("请选择您要购买的商品");
		return false;
	}
	
	if (!/^[0-9]*[1-9][0-9]*$/.test($.trim(quantity))) {
		alert("商品品数量必须为正整数!");
		return false;
	}
	
	
	G_JSON_POST('/shop/cartItem/addCart',{"pId": id, "quantity": quantity},function(data){
				if (data.status == "success") {
					cartDialog();
				} else {
					alert(data.message)
				}
	});
	
	/**
	$.ajax({
		url: woyouhuo.baseUrl + "/shop/cartItem/addCart",
		data: {"pId": id, "quantity": quantity},
		type: "POST",
		dataType: "json",
		cache: false,
		success: function(data) {
			if (data.status == "success") {
				cartDialog();
			} else {
				alert(data.message)
			}
		},error:function(x){
			if(403==x.status){
				window.location.href="/alipay_auth_error";
			}
			console.log(x.status);
		}
	});
	*/
}

function cartDialog(){
	  var $wrap = $('<div class="maskbox-suc" id="cartTip">'
					   +'<P class="ui-icon icon-suc-basket"></P>'
					   +'<p> 加入购物车成功</P></div>');
	  $("#maskbox").append($wrap);

	  $wrap.fadeIn(1200);

	  $wrap.click(function(){
	    $wrap.fadeOut(1200).remove();
	  })

	  setTimeout(function(){
	    $wrap.fadeOut(1000).remove();
	    $("#maskbox").fadeOut(1000);
	    },1300);
}
	
})



